﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iFinity.PropertyAgent.ModuleFriendlyUrlProvider.Entities
{
    internal enum PropertyTypeUrlSource
    {
        PropertyTypeName = 0, PropertyTypeDescription = 1
    }
    internal enum PropertyTypeUrlStyle
    {
        PropertyTypeName = 0, PropertyTypeHierarchy = 1
    }
    /// <summary>
    /// This class holds the options for each tab specified (or for all tabs)
    /// </summary>
    [Serializable()]
    internal class TabUrlOptions
    {
        int _tabId = -1;
        int _startingPropertyId = -1;
        List<string> _customFieldIds;
        PropertyTypeUrlSource _propertyTypeSource;
        PropertyTypeUrlStyle _propertyTypeStyle;
        protected bool _redirectOtherStyles = false;
        #region public properties
        public int TabId
        {
            get { return _tabId; }
            set { _tabId = value; }
        }
        internal List<string> CustomFieldIds
        {
            get { return _customFieldIds; }
            set { _customFieldIds = value; }
        }
        internal int StartingPropertyId
        {
            get
            { return _startingPropertyId; }
            set { _startingPropertyId = value; }
        }
        internal bool RedirectOtherStyles
        {
            get { return _redirectOtherStyles; }
            set { _redirectOtherStyles = value; }
        }
        internal PropertyTypeUrlSource PropertyTypeSource
        {
            get { return _propertyTypeSource; }
            set { _propertyTypeSource = value; }
        }
        internal PropertyTypeUrlStyle PropertyTypeStyle {
            get { return _propertyTypeStyle; }
            set { _propertyTypeStyle = value; }
            }
        #endregion
        internal TabUrlOptions(int tabId, int startingPropertyId, Hashtable customFieldIds, Hashtable propertyTypeSource, Hashtable propertyTypeStyle)
        {
            _tabId = tabId;
            _startingPropertyId = startingPropertyId;
            _customFieldIds = GetListFromString(GetTabSetting(customFieldIds), '|');
            _propertyTypeSource = (PropertyTypeUrlSource)EnumConversion.ConvertFromEnum(typeof(PropertyTypeUrlSource), GetTabSetting(propertyTypeSource), PropertyTypeUrlSource.PropertyTypeName);
            _propertyTypeStyle = (PropertyTypeUrlStyle)EnumConversion.ConvertFromEnum(typeof(PropertyTypeUrlStyle), GetTabSetting(propertyTypeStyle), PropertyTypeUrlStyle.PropertyTypeHierarchy);
        }
        //private constructor for cloning
        internal TabUrlOptions()
        {
        }
        private string GetTabSetting(Hashtable settingList)
        {
            string result = null;
            string tabKey = _tabId.ToString();
            if (settingList != null && settingList.ContainsKey(tabKey))
            {
                result = (string)settingList[tabKey];
            }
            return result;
        }



        internal TabUrlOptions Clone()
        {
            TabUrlOptions clone = new TabUrlOptions();
            clone._propertyTypeStyle = _propertyTypeStyle;
            clone._propertyTypeSource = _propertyTypeSource;
            clone._tabId = _tabId;
            clone._redirectOtherStyles = _redirectOtherStyles;
            clone._customFieldIds = _customFieldIds;
            return clone;
        }
        internal static Hashtable GetHashTableFromSetting(string raw)
        {
            string rawResult;
            return GetHashTableFromSetting(raw, out rawResult);
        }
        internal List<string> GetListFromString(string raw, char sep)
        {
            string[] arr = raw.Split(sep);
            return new List<string>(arr);
        }
        internal static Hashtable GetHashTableFromSetting(string raw, out string rawResult)
        {
            Hashtable result = new Hashtable();
            rawResult = raw;
            if (raw != null && raw != "")
            {
                string[] pairs = raw.Split(';');
                foreach (string pair in pairs)
                {
                    string key = null, val = null;
                    string[] vals = pair.Split(',');
                    if (vals.GetUpperBound(0) >= 0)
                        key = vals[0];
                    if (vals.GetUpperBound(0) >= 1)
                        val = vals[1];
                    if (key != null && val != null)
                        result.Add(key, val);
                }
            }
            return result;
        }
    }
    internal static class EnumConversion
    {
        internal static Enum ConvertFromEnum(Type enumType, string rawValue, Enum defaultValue)
        {
            if (rawValue != null)
            {
                Enum result = defaultValue;
                try
                {
                    object enumVal = Enum.Parse(enumType, rawValue);
                    result = (Enum)Convert.ChangeType(enumVal, enumType);
                }
                catch (Exception ex)
                {
                    //ignore because we can't implement Enum.TryParse until .NET 4
                }
                return result;
            }
            else
                return defaultValue;
            
        }
    }
}
